package com.sl.plat.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.http.ContentType;
import com.sl.common.utils.CommonFile;
import com.sl.water.service.AttachService;
import com.sl.water.service.RehearsalSchemeService;
import com.sl.water.vo.AttachVO;
import io.swagger.v3.oas.annotations.Operation;


import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.media.SchemaProperty;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import io.swagger.v3.oas.annotations.parameters.RequestBody;

import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: duan
 * @time: 2025-05-26 17:10
 */
@RestController
@RequestMapping("/plat/file")
@CrossOrigin
@RequiredArgsConstructor
@Slf4j
@Tag(name = "公共-文件操作", description = "提供文件上传、下载、浏览相关功能")
public class FileController {


    private final AttachService attachService;

    @PostMapping(value = "/upload")
    @Operation(summary = "上传")
    @ApiResponse(responseCode = "200", description = "上传成功", content = @Content(schema = @Schema(implementation = AttachVO.class)))
    public ResponseEntity<?> upload(
            @Parameter(
                    description = "文件",
                    required = true
            )
            @RequestPart(required = true)
            MultipartFile file,
            @Parameter(
                    description = "文件分类,二次根目录",
                    required = true,
                    example = "patrol"
            )
            @RequestParam @NotBlank(message = "文件分类不能为空") String kind) {
       return attachService.upload(kind,file);
    }

    // 文件下载接口
    @GetMapping("/download")
    @Operation(summary = "下载")
    public void downloadFile(
            @Parameter(
                    description = "文件路径",
                    required = true
            )
            @RequestParam String path, HttpServletResponse response) {
        attachService.downloadFile(path, response) ;
    }

    // 文件预览接口
    @GetMapping("/preview")
    @Operation(summary = "预览")
    public ResponseEntity<byte[]> previewFile(
            @Parameter(
                    description = "文件路径",
                    required = true
            )
            @RequestParam String path, HttpServletResponse response) {
       return  attachService.previewFile(path,response);
    }




}
